const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');


const app = getApp()


// pages/Order/Order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
      region: ['省份','城市','区县'],
    checke:1,
    id:''
  },
  // 地区选择
  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
// 默认
  begin(e) {
    if (this.data.checke == 1) {
      this.setData({
        checke: 0
      })
    } else {
      this.setData({
        checke: 1
      })
    }
  },


tijiao(e){
  let that=this
  if(that.data.id==0){
    that.setData({
      id:''
    })
    console.log(e)
    util.request(api.SaveAddress, {
      detaiAddress: e.detail.value.detaiAddress,
      name: e.detail.value.name,
      phone: e.detail.value.phone,
      addTime: "",
      districtCounty: that.data.region[1],
      isDefault: that.data.checke,
      province: that.data.region[0],
      town: that.data.region[2]
    }, 'post', {
        'Content-Type': 'application/json',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        if(res.code==200){
          wx.navigateBack();
        }
       wx.showToast({
         title: res.msg,
         icon: 'none'
       })
      });


  }
 },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id: options.id
    })
    this.getdetail()
  },


getdetail(){
  let that=this
  if(that.data.id!=0){
  util.request(api.AddressDetail, {
    id:that.data.id
  }, 'get', {
      'Content-Type': 'application/json',
      'X-Qxmall-Token': wx.getStorageSync('token')
    }).then(function (res) {
      console.log(res)
      var arr = []
      arr.push(res.data.addressDetail.province)
      arr.push(res.data.addressDetail.districtCounty)
      arr.push(res.data.addressDetail.town)
      console.log(arr)
      that.setData({
        address:res.data.addressDetail,
        region:arr
      })
    });
  }
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})